﻿// HSS.Forms.TextBoxWriter.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       TextBoxWriter.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System.IO;
	using System.Text;
	using System.Windows.Forms;
	#endregion

	#region TextBoxWriter
	/// <summary>
	/// TextBoxWriter receives data written and appends to a TextBox usercontrol
	/// </summary>
	public class TextBoxWriter : TextWriter
	{
		internal TextBoxWriter(TextBox textBox)
			: base()
		{
			_textBox = textBox;
		}

		private TextBox _textBox;

		/// <summary>
		/// TextWriter Encoding
		/// </summary>
		public override Encoding Encoding
		{
			get { return Encoding.Default; }
		}

		private delegate void WriteDelegate(string value);

		/// <summary>
		/// Appends text to the Writer
		/// </summary>
		/// <param name="value">The text to append</param>
		public override void Write(string value)
		{
			if (_textBox.InvokeRequired)
			{
				_textBox.Invoke(new WriteDelegate(Write), new object[] { value });
			}
			else
			{
				_textBox.AppendText(value.Replace("\n", base.NewLine));
			}
		}
		/// <summary>
		/// Appends text to the Writer including a NewLine
		/// </summary>
		/// <param name="value">The text to append</param>
		public override void WriteLine(string value)
		{
			this.Write(value);
			this.Write(base.NewLine);
		}
	}
	#endregion
}